<?php

class Association
{

  /**
   * @var Aggregate
   */
  public $owner;

  /**
   * @var AggregateModel
   */
  public $source;

  /**
   * @var DomObjModel
   */
  public $target;

  /**
   * @var string
   */
  public $name;

  /**
   * @var array
   */
  public $model;

  /**
   * @var boolean
   */
  public $dataLoaded;

  public function __construct(Aggregate $owner, $name, $target)
  {
    $this->owner = $owner;
    $this->source = $owner;
    $this->target = $target;
    $this->name = $name;

    $this->model = Domain::getObject($this->source)->getAssociation($this->name);
  }

  /**
   *
   * @return AssociationModel
   */
  public function getModel()
  {
    return $this->model;
  }

  public function loadData()
  {
  }

  public function unloadData()
  {
    unset($this->owner->{$this->name});
  }

  public function save()
  {
  }

  public function getPrimaryKey()
  {
    if (! isset($this->model['primaryKey'])) {
      $this->model['primaryKey'] = Domain::getObject($this->source)->getPkField()->getName();
    }

    return $this->model['primaryKey'];
  }

  public function getForeignKey()
  {
    if (! isset($this->model['foreignKey'])) {
      $this->model['foreignKey'] = Domain::getObject($this->target)->getPkField()->getName();
    }

    return $this->model['foreignKey'];
  }

}
